package tliasmanagement.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tliasmanagement.mapper.StudentMapper;
import tliasmanagement.pojo.PageResult;
import tliasmanagement.pojo.Student;
import tliasmanagement.pojo.StudentQueryParam;
import tliasmanagement.service.StudentService;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 分页查询
     * @param studentQueryParam 查询参数
     * */
    @Override
    public PageResult page(StudentQueryParam studentQueryParam) {
        // 分页查询
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        List<Student> list = studentMapper.getStudentByPage(studentQueryParam);

        Page<Student> p = (Page<Student>) list;
        return new PageResult(p.getTotal(), p.getResult());
    }

    /**
     * 新增学生
     * @param student 新增的学生
     * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.add(student);
    }

    @Override
    public Student getDetailInfo(Integer id) {
        return studentMapper.getDetailInfo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }
}
